<template>
 <div class="e-form">
    <div class="tabs">
        <el-tabs tab-position="left" v-model="tabsName" @tab-click="onChangeTab">
            <el-tab-pane label="基本信息" name="baseInfo" :disabled="clickTabFlag">
            </el-tab-pane>
            <el-tab-pane label="主体信息" name="mainInfo" :disabled="clickTabFlag">
            </el-tab-pane>
            <el-tab-pane label="清单信息" name="contractList" :disabled="clickTabFlag">
            </el-tab-pane>
            <el-tab-pane label="附件信息" name="attach" :disabled="clickTabFlag">
            </el-tab-pane>
            <div>
                <!-- 基本信息 -->
                <div id="baseInfoCon" class="con">
                    <div class="tabs-title" id="baseInfo">基本信息</div>
                    <div style="width: 100%" class="form">
                        <el-form :model="formData" :label-width="formLabelWidth">
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="合同名称：">
                                        <span>{{formData.billName}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="合同编号：">
                                        <span>{{formData.originalBillNo}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="项目名称：">
                                        <span>{{formData.projectName}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="合同变更编号：">
                                        <span>{{formData.billNo}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="签订日期：">
                                        <span>{{formData.signingDate}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="合同工期(月)：">
                                        <span>{{formData.contractDuration}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="税率%：">
                                        <span>{{formData.taxRate}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="批复状态">
                                        <el-radio-group v-model="formData.replyState">
                                            <el-radio :label="0">待批复</el-radio>
                                            <el-radio :label="1">已批复</el-radio>
                                        </el-radio-group>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原不含税金额(元)：">
                                        <span>{{formData.originalAmountExcludingTax}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后不含税金额(元)：">
                                        <span>{{formData.amountExcludingTax}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原税额(元)：">
                                        <span>{{formData.originalTaxAmount}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后税额(元)：">
                                        <span>{{formData.taxAmount}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原合同净价(元)：">
                                        <span>{{formData.originalNetContractPrice}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后合同净价(元)：">
                                        <span>{{formData.netContractPrice}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原暂列金额(元)：">
                                        <span>{{formData.originalProvisionalSum}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后暂列金额(元)：">
                                        <span>{{formData.provisionalSum}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="原合同金额(元)：">
                                        <span>{{formData.originalContractAmount}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="变更后合同金额(元)：">
                                        <span>{{formData.contractAmount}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="变更次数：">
                                        <span>{{formData.changeCount}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="单价类型：">
                                        <span>{{['含税单价','不含税单价'][formData.priceType]}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="币种：">
                                        <span>{{formData.currencyName}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="金额保留位数：">
                                        <span>{{formData.amountReservedDigits}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="签约人：">
                                        <span>{{formData.signatory}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="人民币汇率：">
                                        <span>{{formData.rmbRate}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="签订地址：">
                                        <span>{{formData.detailedAddress}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="管理单位：">
                                        <span>{{formData.managementUnit}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="备注：">
                                        <span>{{formData.remarks}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="经办人：">
                                        <span>{{formData.handler}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </div>
                </div>

                <!-- 主体信息 -->
                <div id="mainInfoCon" class="con">
                    <div class="tabs-title" id="mainInfo">主体信息</div>
                    <div style="width:100%" class="form">
                        <el-form :model="formData" :label-width="formLabelWidth">
                            <div class="mainTitle">甲方信息</div>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="名称：" >
                                        <span>{{mainInfo.partyAName}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="法人代表：">
                                        <span>{{mainInfo.partyALegalRepresentative}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="统一社会信用代码：" >
                                        <span>{{mainInfo.partyACreditCode}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="开户银行：" >
                                        <span>{{mainInfo.partyADepositBank}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="委托代理：" >
                                        <span>{{mainInfo.aname}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="开户账号：" >
                                        <span>{{mainInfo.partyADepositCode}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="联系电话：">
                                        <span>{{mainInfo.partyAPhone}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="邮编：">
                                        <span>{{mainInfo.partyAZipCode}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="地址：">
                                        <span>{{mainInfo.partyADetailedAddress}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="传真：" >
                                        <span>{{mainInfo.partyAFax}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <div class="mainTitle">乙方信息</div>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="名称：" >
                                        <span>{{mainInfo.partyBName}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="法人代表：">
                                        <span>{{mainInfo.partyBLegalRepresentative}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="统一社会信用代码：" >
                                        <span>{{mainInfo.partyBCreditCode}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="开户银行：" >
                                        <span>{{mainInfo.partyBDepositBank}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="委托代理：" >
                                        <span>{{mainInfo.partyBPrincipalAgent}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="开户账号：" >
                                        <span>{{mainInfo.partyBDepositCode}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="联系电话：">
                                        <span>{{mainInfo.partyBPhone}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="邮编：">
                                        <span>{{mainInfo.partyBZipCode}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="地址：">
                                        <span>{{mainInfo.partyBDetailedAddress}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="传真：" >
                                        <span>{{mainInfo.partyBFax}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <div class="mainTitle">公正信息</div>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="公正机关：">
                                        <span>{{mainInfo.impartialOrgan}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="公正日期：">
                                        <span>{{mainInfo.impartialDate}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="开户银行：">
                                        <span>{{mainInfo.impartialBank}}</span>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="开户账号：">
                                        <span>{{mainInfo.impartialDepositCode}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="公正意见：">
                                        <span>{{mainInfo.impartialOpinion}}</span>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </div>
                </div>

                <!-- 合同清单  -->
                <div id="contractListCon" class="con">
                    <div class="tabs-title" id="contractList">清单信息</div>
                    <div class="e-table">
                        <div class="top">
                            <div class="left">
                                <div>
                                    <el-button size="small" type="primary" plain>新增业主下发清单</el-button>
                                    <el-button size="small" type="primary" plain>Excel导入业主下发清单</el-button>
                                    <el-button size="small" type="danger" plain>删除细目</el-button>
                                </div>
                            </div>
                        </div>
                        <el-table
                            :data="contractListTable"
                            border
                            style="width: 100%; margin-bottom: 20px"
                        >
                            <el-table-column prop="" label="细目编号" width="200">
                            </el-table-column>
                            <el-table-column prop="" label="细目名称" width="120">
                            </el-table-column>
                            <el-table-column prop="" label="计量单位" width="120">
                            </el-table-column>
                            <el-table-column prop="" label="业主下发数量" width="150">
                            </el-table-column>
                            <el-table-column prop="" label="含税金额(元)" width="130">
                            </el-table-column>
                            <el-table-column prop="" label="结构名称" width="130">
                            </el-table-column>
                            <el-table-column prop="" label="内部标准数量">
                            </el-table-column>
                        </el-table>
                    </div>
                </div>

                <!-- 附件信息 -->
                <div id="attachCon" class="con">
                    <div class="tabs-title" id="attach">附件信息</div>
                    <attach-comp :relationId="attachId" bucketName="ownercontract" isView />
                </div>
            </div>
        </el-tabs>
    </div>
    <div class="buttons">
        <el-button type="primary" size="small" style="background: #2e61d7"
            >打印</el-button
        >
        <el-button type="primary" size="small" style="background: #2e61d7"
            >审核</el-button
        >
        <el-button type="primary" size="small" style="background: #2e61d7"
            >保存</el-button
        >
        <el-button type="primary" size="small" style="background: #2e61d7"
            >保存并提交</el-button
        >
        <el-button  type="danger" size="small"
            >撤回</el-button
        >
        <el-button  type="danger" size="small"
            >删除</el-button
        >
        <el-button type="danger" size="small"
            >作废</el-button
        >
        <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
    <div id="seat" :style="{height: seatHeight + 'px'}"></div>
 </div>
</template>

<script>
import '@/utils/jquery.scrollTo.min'
import { mapState } from 'vuex'
import fn from '@/utils/checkEmpty.js'
import ownercontract from '@/api/balanceContractApi/ownercontract.js'
import AttachComp from '@/components/common/attachIndex'
export default {
    data () {
        return {
            //基本信息表单数据
            formData: {
                'amountExcludingTax': null,
                'amountReservedDigits': '',
                'area': '',
                'billId': '',
                'billName': '',
                'billNo': '',
                'changeCount': null,
                'city': '',
                'contractAmount': null,
                'contractDuration': null,
                'country': '',
                'currencyId': '',
                'currencyName': '',
                'detailedAddress': '',
                'handler': '',
                'managementUnit': '',
                'netContractPrice': null,
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDesciption': '',
                'nullifyReason': '',
                'orgId': '',
                'orgName': '',
                'originalAmountExcludingTax': null,
                'originalBillNo': '',
                'originalContractAmount': null,
                'originalNetContractPrice': null,
                'originalProvisionalSum': null,
                'originalTaxAmount': null,
                'priceType': null,
                'projectId': '',
                'projectName': '',
                'province': '',
                'provisionalSum': null,
                'remarks': '',
                'replyState': null,
                'rmbId': '',
                'rmbName': '',
                'rmbRate': null,
                'signatory': '',
                'signingDate': '',
                'state': null,
                'taxAmount': null,
                'taxRate': null,
                'workId': ''
            },
            //主体信息表单数据
            mainInfo: {
                'billId': '',
                'impartialBank': '',
                'impartialDate': '',
                'impartialDepositCode': '',
                'impartialOpinion': '',
                'impartialOrgan': '',
                'partyAArea': '',
                'partyACity': '',
                'partyACountry': '',
                'partyACreditCode': '',
                'partyADepositBank': '',
                'partyADepositCode': '',
                'partyADetailedAddress': '',
                'partyAFax': '',
                'partyALegalRepresentative': '',
                'partyAName': '',
                'partyAPhone': '',
                'partyAPrincipalAgent': '',
                'partyAProvince': '',
                'partyAZipCode': '',
                'partyBArea': '',
                'partyBCity': '',
                'partyBCountry': '',
                'partyBCreditCode': '',
                'partyBDepositBank': '',
                'partyBDepositCode': '',
                'partyBDetailedAddress': '',
                'partyBFax': '',
                'partyBLegalRepresentative': '',
                'partyBName': '',
                'partyBPhone': '',
                'partyBPrincipalAgent': '',
                'partyBProvince': '',
                'partyBZipCode': ''
            },
            formLabelWidth: '190px',
            tableData1: [],
            tabsName: 'baseInfo',
            screenWidth: 0,
            screenHeight: 0,
            lastConHeight: 0,
            clickTabFlag: false, // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            attachTable: [], //附件信息表格数据
            contractListTable: [], //清单信息表格数据
            objFlag: {   //判断是否转圈圈
                flag1: false,
                flag2: false,
                flag3: false,
                flag4: false,
            },
            billid: '',
        }
    },
    mounted () {
        //this.getBasic()
        // 获取最后一个内容区域的高度，计算底部空白
        this.getLastConHeight()
        // 保存所有tabName
        const arr = ['baseInfo', 'mainInfo', 'contractList', 'attach']

        let $idsTop = []
        window.addEventListener('scroll', () => {
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            if (this.clickTabFlag) {
                return
            }

            if (!$idsTop[$idsTop.length - 1]) {
                $idsTop = arr.map(item => {
                    const itemTop = document.getElementById(item).offsetTop
                    return itemTop
                })
            }
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            // 倒序查找
            let curLocal = 0
            for (let len = $idsTop.length, i = len - 1; i >= 0; i--) {
                let item = $idsTop[i]
                if ((scrollTop + 1) >= item) {
                    curLocal = i
                    break
                }
            }
            // 设置对应tabName
            this.tabsName = arr[curLocal]
            this.queryData(this.tabsName)
        })

        this.screenWidth = document.documentElement.clientWidth
        this.screenHeight = document.documentElement.clientHeight
        window.addEventListener('resize', () => {
            this.screenWidth = document.documentElement.clientWidth
            this.screenHeight = document.documentElement.clientHeight
            $idsTop = arr.map(item => {
                const itemTop = document.getElementById(item).offsetTop
                return itemTop
            })
        })
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify
        }),
        // 填补底部空白，以使高度够滚动
        seatHeight () {
            return this.screenHeight - 72 - this.lastConHeight
        },
        attachId () {
            return this.$route.query.billid
        }
    },
    methods: {
        //取消
        handleClose () {
            this.$router.go(-1)
        },
        //判断是否为空
        checkEmpty (datas) {
            return fn.checkEmpty(datas)
        },
        //根据点击不同的tab,调用不同的接口
        queryData (name) {
            switch(name) {
            case 'baseInfo':
                // if(this.checkEmpty(this.formData)) this.getBasic()
                break
            case 'mainInfo':
                //if(this.checkEmpty(this.mainInfo)) this.getMain()
                break
            case 'contractList':
                if(this.checkEmpty(this.contractListTable)) this.getContractList()
                break
            case 'attach':
                if(this.checkEmpty(this.attachTable)) this.getAttach()
                break
            }
        },
        //根据id查询基本信息
        getBasic () {
            this.billid = this.$route.query.billid
            ownercontract.findByIdChangeBasic(this.billid).then(res=>{
                this.formData = res
                this.objFlag.flag1 = true
            })
        },
        //根据id查询主体信息
        getMain () {
            ownercontract.getEssentialInformationChangeById(this.billid).then(res=>{
                this.mainInfo = res
                this.objFlag.flag2 = true
            })
        },
        //根据id查询清单信息
        getContractList () {
            this.objFlag.flag3 = true
        },
        //根据id查询附件信息
        getAttach () {
            this.objFlag.flag4 = true
        },
        onChangeTab (e) {
            $.scrollTo(`#${e.name}`, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
            this.queryData(e.name)
        },
        // 获取最后一个内容区域的高度，计算底部空白
        getLastConHeight () {
            let si = setInterval(() => {
                // 因为dom异步加载，通过轮询找到渲染后的dom，获取高度
                if (document.getElementById('recodesCon')) {
                    const lastConHeight = document.getElementById('recodesCon').offsetHeight
                    console.log('lastConHeight: ', lastConHeight)
                    this.lastConHeight = lastConHeight
                    clearInterval(si)
                    si = null
                }
            }, 100)
        },
    },
    components: {
        AttachComp
    }
}

</script>

<style lang='scss' scoped>
.mainTitle{
    box-sizing: border-box;
    width:100%;
    height: 40px;
    line-height: 40px;
    background: rgb(246,246,246);
    border: 1px solid rgb(236, 236, 236);
    margin: auto;
    margin-bottom: 15px;
    padding-left: 10px;
}
.separ{
   width: 30px;
   height: 40px;
   line-height: 18px;
   text-align: center;
}
.e-table{
    min-height: auto;
    background: #fff;
}
.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}

::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}
::v-deep.el-table .cell {
    text-align: center;
}
</style>